package test;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client extends SocketBase {
	
	public Client(Socket socket) throws IOException {
		super(socket);
		System.out.println(Client.class + "listening on port " + socket.getPort());
	}

	private static int port = 1444;
	private static String host = "localhost";

	public static void main(String args[]) throws IOException {
		int thisPort = Client.port;
		String thisHost = Client.host;
		if(args.length == 2) {
			try {
				thisPort = Integer.parseInt(args[1]);
			} catch (NumberFormatException e) {
				//do nothing
			}
			thisHost = args[0];
		}
		try {
			Client client = new Client(new Socket(thisHost, thisPort));
			client.write("Test string");
			client.write("Test x2");
			client.closeAndNotify();
			System.out.println("Client closed socket");
		} catch (UnknownHostException e) {
			System.err.println("Unknown host " + host);
			System.exit(-1);
		} catch (IOException e) {
			System.err.println("Could not get I/O for connection to " + thisHost);
			System.exit(-1);
		}
		System.exit(0);
	}
	
	protected void closeAndNotify() {
		write(SocketBase.close);
		super.close();
	}

}
